//
//  YWMyPostTroublesViewController.m
//  maintain
//
//  Created by Kyo on 16/9/22.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMyPostTroublesViewController.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "DLTabedSlideView.h"
#import "DLFixedTabbarView.h"
#import "Masonry.h"
#import "YWCommonTool.h"

#import "YWUnAssignPostTroubleTableViewController.h"
#import "YWAssignPostTroubleTableViewController.h"
#import "YWAllMyPostTroublesTableViewController.h"
#import "YWReportTroubleSubscriptionTableViewController.h"

#import "YWEwinManager.h"
#import "YWQrcodeManager.h"
#import "YWNoQrcodePostTroubleTableViewController.h"
#import "YWEquipmentManager.h"
#import "YWPostTroubleTableViewController.h"

#import "YWUnReadCount.h"
#import "YWLoginUser.h"
#import "YWLoginTool.h"

@interface YWMyPostTroublesViewController ()<DLTabedSlideViewDelegate>
@property (nonatomic, weak) DLTabedSlideView* tabedSlideView;
@property (nonatomic, strong) NSMutableArray *menuNameArray;
@property (nonatomic, strong) UIButton *scanButton;
@property (nonatomic, strong) UIButton *noScanButton;
@end

@implementation YWMyPostTroublesViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = NSLocalizedString(@"postTrouble", nil);//@"报障";
    self.view.backgroundColor = [UIColor customBackgroudColor];
    
    //这里的右上角录入报障的菜单取消，点击无码进入录入报障
//    if (self.menuNameArray.count>0) {
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickRightBarButtonEvent:)];
//    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:NSLocalizedString(@"subscription", nil) style:UIBarButtonItemStylePlain target:self   action:@selector(subscriptionActionEvent)];
    [self autoLayoutView];//@"订阅"
}

- (NSMutableArray *)menuNameArray{
    if (!_menuNameArray) {
        _menuNameArray = [[NSMutableArray alloc] initWithArray:[[YWEwinManager shareManager] ewinMenuWithMenuEnName:ewinMenuEnName_report_malfunction]];
    }
    return _menuNameArray;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 开启
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
- (DLTabedSlideView*)tabedSlideView
{
    if (!_tabedSlideView) {
        DLTabedSlideView* tabedSlideView = [[DLTabedSlideView alloc] init];
        tabedSlideView.delegate = self;
        tabedSlideView.baseViewController = self;
        tabedSlideView.tabItemNormalColor = [UIColor blackColor];
        tabedSlideView.tabItemSelectedColor = [UIColor customRedColor];
        tabedSlideView.tabbarTrackColor = [UIColor customRedColor];
        tabedSlideView.backgroundColor = [UIColor whiteColor];
        YWUnReadCount *totalCount = [[YWEwinManager shareManager] totalCount];
        YWLoginUser *currentUser = [YWLoginTool loginUser];
        NSString *title;
        if (currentUser.isTroubleServer) {
            title = totalCount.troubleTotal>0?[NSString stringWithFormat:@"%@(%ld)",NSLocalizedString(@"reportTroubleSendBill", nil),(long)totalCount.troubleTotal]:NSLocalizedString(@"reportTroubleSendBill", nil);//@"派单";
        }else{
            title = [NSString stringWithFormat:NSLocalizedString(@"reportTroubleSendBill", nil)];
        }
        DLTabedbarItem* unfinishItem = [DLTabedbarItem itemWithTitle:title image:nil selectedImage:nil];
        DLTabedbarItem* finishItem = [DLTabedbarItem itemWithTitle:[NSString stringWithFormat:@"已%@",NSLocalizedString(@"reportTroubleSendBill", nil)] image:nil selectedImage:nil];//已派单
        DLTabedbarItem* myTroublesItem = [DLTabedbarItem itemWithTitle:[NSString stringWithFormat:@"我的%@",NSLocalizedString(@"postTrouble", nil)] image:nil selectedImage:nil];//我的报障
        tabedSlideView.tabbarItems = @[ myTroublesItem, unfinishItem, finishItem ];
        [tabedSlideView buildTabbar];
        if (currentUser.isTroubleServer) {
            tabedSlideView.selectedIndex = 1;
        }else{
            tabedSlideView.selectedIndex = 0;
        }
        [self.view addSubview:tabedSlideView];
        _tabedSlideView = tabedSlideView;
    }
    return _tabedSlideView;
}

- (void)autoLayoutView
{
    [self.tabedSlideView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.view.mas_top);//.offset(64);
        make.left.mas_equalTo(self.view.mas_left);
        make.height.mas_equalTo(self.view.mas_height);//.offset(-64);
        make.width.mas_equalTo(self.view.mas_width);
    }];
    [self.view addSubview:self.noScanButton];
    [self.view addSubview:self.scanButton];
}

- (NSInteger)numberOfTabsInDLTabedSlideView:(DLTabedSlideView*)sender
{
    return self.tabedSlideView.tabbarItems.count;
}

- (UIViewController*)DLTabedSlideView:(DLTabedSlideView*)sender controllerAt:(NSInteger)index
{
    switch (index) {
        case 0: {
            YWAllMyPostTroublesTableViewController* allMyTroublesVC = [[YWAllMyPostTroublesTableViewController alloc] init];
            return allMyTroublesVC;
        }
        case 1: {
            YWUnAssignPostTroubleTableViewController* unfinishVC = [[YWUnAssignPostTroubleTableViewController alloc] init];
            return unfinishVC;
        }
        case 2: {
            YWAssignPostTroubleTableViewController* finishVC = [[YWAssignPostTroubleTableViewController alloc] init];
            return finishVC;
        }
        default:
            return nil;
    }
}

- (UIButton *)scanButton{
    if (!_scanButton) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        //[scanButton setImage:[UIImage imageNamed:@"report_malfunction_normal"] forState:UIControlStateNormal];
        //scanButton.layer.masksToBounds = YES;
        [button setTitle:NSLocalizedString(@"scanQrcode", nil) forState:UIControlStateNormal];//扫码
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor customBlueColor];
        [button addTarget:self action:@selector(handleScanBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(SCREEN_WIDTH/4, SCREEN_HEIGHT-64-KbottomViewH, SCREEN_WIDTH/4*3, KbottomViewH);
        _scanButton = button;
    }
    return _scanButton;
}
- (UIButton *)noScanButton{
    if (!_noScanButton) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:NSLocalizedString(@"recordReportTrouble", nil) forState:UIControlStateNormal];//录入报障
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor customYellowColor];
        [button addTarget:self action:@selector(handleNoScanBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, SCREEN_HEIGHT-64-KbottomViewH, SCREEN_WIDTH/4, KbottomViewH);
        _noScanButton = button;
    }
    return _noScanButton;
}

- (void)handleNoScanBtnEvent:(id)sender{
    YWNoQrcodePostTroubleTableViewController *noQrcodePostTroubleVC = [[YWNoQrcodePostTroubleTableViewController alloc] init];
    [self.navigationController pushViewController:noQrcodePostTroubleVC animated:YES];
}

- (void)handleScanBtnEvent:(id)sender{
    //扫码去报障
    [[YWQrcodeManager shareManager] clickScanButtonWithType:YWQrcodeScanTypeNomall ViewController:self completionBlock:^(NSString *qrcodeId, QRCodeReaderViewController *qrCodeReaderViewController) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSArray *allEqu = [[YWEquipmentManager shareManager] equipmentWithQrcodeId:qrcodeId];
            [qrCodeReaderViewController dismissViewControllerAnimated:NO completion:^{
                YWPostTroubleTableViewController * postTroublevc = [[YWPostTroubleTableViewController alloc] init];
                postTroublevc.currentEquipment = allEqu.count ? allEqu[0] : nil;
                postTroublevc.equipmentArray = allEqu;
                postTroublevc.qrCodeId = qrcodeId;
                postTroublevc.shouldPopBack = YES;
                [self.navigationController pushViewController:postTroublevc animated:YES];
            }];
        });
    }];
}

- (void)subscriptionActionEvent{
    YWReportTroubleSubscriptionTableViewController * subscrVC = [[YWReportTroubleSubscriptionTableViewController alloc] init];
    [self.navigationController pushViewController:subscrVC animated:YES];
}

@end
